package com.greenDao.activity.tool;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.util.SparseIntArray;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;

import com.greenDao.db.GreenDaoHelper;
import com.greenDao.pojo.Event;
import com.greenDao.pojo.Movie;
import com.greenDao.pojo.MovieDao.Properties;

import de.greenrobot.dao.query.QueryBuilder;

public class DatabaseUtils {

	private DatabaseUtils() {
		// no instance needed
	}

	public static List<Movie> findMoviesByTitle(String title) {
		QueryBuilder<Movie> builder = GreenDaoHelper.getMovieDao().queryBuilder();
		return builder.where(Properties.Title.eq(title)).build().list();
	}

	public static List<Event> findEventsByName(String name) {
		QueryBuilder<Event> builder = GreenDaoHelper.getEventDao().queryBuilder();
		return builder.where(com.greenDao.pojo.EventDao.Properties.Name.eq(name)).build().list();
	}

	public static Movie findMovieById(Long id) {
		QueryBuilder<Movie> builder = GreenDaoHelper.getMovieDao().queryBuilder();
		return builder.where(Properties.Id.eq(id)).build().list().get(0);
	}

	public static Event findEventById(Long id) {
		QueryBuilder<Event> builder = GreenDaoHelper.getEventDao().queryBuilder();
		return builder.where(Properties.Id.eq(id)).build().list().get(0);
	}

	public static List<Event> findEventsByMovieId(Long movieId) {
		QueryBuilder<Event> builder = GreenDaoHelper.getEventDao().queryBuilder();
		return builder.where(com.greenDao.pojo.EventDao.Properties.MovieId.eq(movieId)).build().list();
	}

	public static void updateMovie(Movie movie) {
		GreenDaoHelper.getMovieDao().update(movie);
	}

	public static void updateEvent(Event event) {
		GreenDaoHelper.getEventDao().update(event);
	}

	public static void removeMovie(Movie movie) {
		GreenDaoHelper.getEventDao().deleteInTx(DatabaseUtils.findEventsByMovieId(movie.getId()));
		GreenDaoHelper.getMovieDao().delete(movie);
	}

	public static void updateMovieById(long id, Movie change) {
		Movie movie = DatabaseUtils.findMovieById(id);
		movie.setTitle(change.getTitle());
		movie.setDirector(change.getDirector());
		movie.setYear(change.getYear());
		movie.setRate(change.getRate());
		DatabaseUtils.updateMovie(movie);
	}

	public static void updateEventById(long id, Event change) {
		Event event = DatabaseUtils.findEventById(id);
		event.setName(change.getName());
		event.setMovieId(change.getMovie().getId());
		event.setTime(change.getTime());
		event.setComment(change.getComment());
		DatabaseUtils.updateEvent(event);
	}

	public static List<Movie> getAllMovies() {
		return GreenDaoHelper.getMovieDao().loadAll();
	}

	public static List<Event> getAllEvents() {
		return GreenDaoHelper.getEventDao().queryDeep("");
	}

	public static long saveMovie(Movie movie) {
		return GreenDaoHelper.getMovieDao().insert(movie);
	}

	public static long saveEvent(Event event) {
		return GreenDaoHelper.getEventDao().insert(event);
	}

	public static void removeEvent(Event event) {
		GreenDaoHelper.getEventDao().delete(event);
	}

	public static SpinnerAdapter createAdapter(Activity activity, SparseIntArray positionToId) {
		List<Movie> movies = DatabaseUtils.getAllMovies();
		String[] titles = Utils.extractDataFromMovies(false, movies, positionToId);
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_item,
				Arrays.asList(titles));
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		return dataAdapter;
	}
}
